var _page = $(".page");
// 加载最新单身列表

var thisload = bottlecms.load({
    done: function (page, next) {
        bottlecms.get("/m/ajax/momen/list", {
            page: page,
            orderBy: $("#orderBy").val()
        }, function (data) {
            var lis = [];
            for (var o in data.list) {
                lis.push(moments_list_template(data.list[o]))
            }
            next(lis.join(''), lis.length);
        })
    }
}).init();


// 点赞
$(".moments_list").on('click', '.item .js-praise', function () {
    var _this = this;
    var moment_id = $(_this).attr('data-id');
    if ($(_this).hasClass('on')) {
        $(_this).removeClass('on');
        bottlecms.get('/m/ajax/momen/unpraise/' + moment_id, null, function (data) {
            if (data.success == true) {
                $(_this).text(bottlecms.parseInt($(_this).text()) - 1)
                bottlecms.toast(data.msg);
            } else {
                $(_this).toggleClass('on');
                bottlecms.toast(data.msg);
            }
        }, function () {
            $(_this).toggleClass('on');
        });
    } else {
        $(_this).addClass('on');
        bottlecms.get('/m/ajax/momen/praise/' + moment_id, null, function (data) {
            if (data.success == true) {
                $(_this).text(bottlecms.parseInt($(_this).text()) + 1)
                bottlecms.toast(data.msg);
            } else {
                $(_this).toggleClass('on');
                bottlecms.toast(data.msg);
            }
        }, function () {
            $(_this).toggleClass('on');
        });
    }
})

function sumitcomment(moment_id, content, rfun) {
    bottlecms.post('/m/ajax/momen/comment',
        {
            momentId:moment_id,
            content: content
        }, function (data) {
            if (data.success == true) {
                if (rfun) return rfun(data);
            } else {
                bottlecms.toast(data.msg);
            }
        });
}

//点击弹出评论框
$(_page).on('click', '.moments_list .item .js-comment', function () {
    var _this = this;
    var moment_id = $(_this).attr('data-id');
    var html = '<div class="set_comment">';
    html += '    <div class="textbox">';
    html += '        <textarea class="" name="content" placeholder="输入评论内容……"></textarea>';
    html += '    </div>';
    html += '    <div class="foot">';
    html += '        <div class="left"></div>';
    html += '        <div class="btnbox"><button class="submit btn btn-sm ">提交</button></div>';
    html += '    </div>';
    html += '</div>';

    var popup_modal = bottlecms.modal({content: '<div class="write_comment_pop">' + html + '</div>', type: 'bottom'});

    $(popup_modal.thispop).find('textarea[name=content]').focus();

    // 提交评论
    $(popup_modal.thispop).on('click', '.set_comment .submit', function () {
        var set_comment = $(this).parents(".set_comment");
        var content = set_comment.find("textarea[name=content]").val();

        window.console.log(content);
        if (content.length < 1) return bottlecms.toast("请填写内容");

        sumitcomment(moment_id, content, function (data) {
            bottlecms.toast(data.msg);
            $(_page).find(".js-comment[data-id='" + moment_id + "']").text(bottlecms.parseInt($(".js-comment[data-id='" + moment_id + "']").text()) + 1)
            popup_modal.close();
        })

    });


});

// 删除
$(document).on('click', '.moments_list .item .js-del', function () {
    var _this = this;
    var moment_id = $(_this).attr('data-id');
    bottlecms.confirm("确定要删除吗？", function () {
        bottlecms.get('/m/ajax/momen/delete/' + moment_id, null, function (data) {
            if (data.success == true) {
                bottlecms.toast(data.msg);
                $(_this).parents('.item').remove();
            } else {
                bottlecms.toast(data.msg);
            }
        });
    })
});

// 预览图片
$(_page).on('click', '.moments_list .item .picbox .pic', function () {
    var photos = [];
    $(this).parent(".picbox").find('img').each(function (index, el) {
        var src = $(this).attr('big-src');
        var w = ($(this)[0].naturalWidth || 200);
        var h = ($(this)[0].naturalHeight || 200);
        photos.push({
            src: src,
            w: ((w > 200 || h > 200) ? w * 2 : w * 5),
            h: ((w > 200 || h > 200) ? h * 2 : h * 5)
        });
    });

    var index = $(this).parent(".picbox").find('.pic').index(this) || 0;
    bottlecms.photoBrowser({
        photos: photos,
    }).open(index)
});
